package com.hmily.dubbo.consumer.sync;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

/**
 * <h1>泛化调用 -- Json 字符串类型传参调用.</h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2021-11-01
 */
public class GenericInvokeAsyncJsonMapConsumerApplication {

    public static void main(String[] args) throws Exception {
        // 设置应用服务名称
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-consumer");
        // 设置注册中心的地址
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");

        // 创建引用 org.apache.dubbo.rpc.service.GenericService 这个服务的对象
        ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setCheck(false);
        referenceConfig.setTimeout(1111);

        referenceConfig.setInterface("com.hmily.dubbo.api.UserQueryFacade");
        referenceConfig.setGeneric(true);

        // 如果想要做到非常非常的通用的话，那么意味着string是一个万能的传参类型，比如这里是某种通用的业务功能，有这么一个json
        // HashMap jsonMap = JSON.parseObject("json", HashMap.class);
        HashMap jsonMap = new HashMap();
        jsonMap.put("id", "G314");
        jsonMap.put("name", "hmilyylimh");
        jsonMap.put("sex", "M");

        // 直接拿到引用的代理对象，然后进行远程调用
        GenericService genericService = referenceConfig.get();
        CompletableFuture<Object> completableFuture = genericService.$invokeAsync("queryUserInfo",
                new String[]{"com.hmily.dubbo.api.bean.UserInfo"},
                new Object[]{jsonMap});
        System.out.println("==========" + completableFuture.get());

        // 阻塞等待
        System.in.read();
    }
}
